package auto.app.controller;


import auto.app.controller.from.AutoExtractionAddFrom;
import auto.app.controller.from.AutoExtractionAddListFrom;
import auto.app.controller.from.AutoExtractionDeleteListFrom;
import auto.app.exetion.HibernateExetion;
import auto.app.exetion.MainExection;
import auto.app.model.AutoExtraction;
import auto.app.service.AutoExtractionService;
import auto.app.util.CommonUtil;
import auto.app.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 杨森
 * @since 2022-07-22
 */
@RestController
@RequestMapping("/autoExtraction")
@Api(value = "关联提取",tags = "关联提取")
public class AutoExtractionController {
    @Autowired
    private AutoExtractionService autoExtractionService;

    @PostMapping(value = "/add",name = "新增关联列表")
    @ApiOperation(value = "新增关联列表",notes = "新增关联列表")
    public Result<Boolean> addExtraction(@Valid @RequestBody AutoExtractionAddListFrom autoExtractionAddListFrom) throws MainExection {
        Boolean aBoolean = autoExtractionService.addExtraction(autoExtractionAddListFrom);
        return Result.getInstance(Boolean.class).setData(aBoolean);
    }

    @PostMapping(value = "/addone",name = "新增单个关联")
    @ApiOperation(value = "新增单个关联",notes = "新增单个关联")
    public Result<Boolean> addExtracitonOne(@Valid @RequestBody AutoExtractionAddFrom autoExtractionAddFrom)  {
        AutoExtraction autoExtraction = CommonUtil.copyOne(autoExtractionAddFrom, AutoExtraction.class);
        autoExtraction.setCreateDate(LocalDateTime.now());
        boolean save = autoExtractionService.save(autoExtraction);
        return Result.getInstance(Boolean.class).setData(save);
    }

    @DeleteMapping(value = "/{id}",name = "删除单个关联提取")
    @ApiOperation(value = "删除单个关联提取",notes = "删除单个关联提取")
    public Result<Boolean> deleteExtraction(@NotNull @PathVariable Integer id) throws MainExection {
        Boolean aBoolean = autoExtractionService.deleteExtraction(id);
        return Result.getInstance(Boolean.class).setData(aBoolean);
    }

    @PostMapping(value = "/delList",name = "删除多个关联")
    @ApiOperation(value = "删除多个关联")
    public Result<Boolean> deleteList(@RequestBody AutoExtractionDeleteListFrom autoExtractionDeleteListFrom) throws MainExection {
        Boolean aBoolean = autoExtractionService.deleteExtractionList(autoExtractionDeleteListFrom.getIds());
        return Result.getInstance(Boolean.class).setData(aBoolean);
    }

    @GetMapping(value = "/list/{caseId}",name = "关联提取列表")
    @ApiOperation(value = "关联提取列表",notes = "关联提取列表")
    public Result<List<AutoExtraction>> list(@PathVariable Integer caseId) throws MainExection {
        List<AutoExtraction> autoExtractions = autoExtractionService.list(caseId);
        return Result.getInstanceList(AutoExtraction.class).setData(autoExtractions);
    }
}

